package com.emep.changzhi.analyse.http.basics;

import com.emep.changzhi.analyse.entity.basics.District;
import com.emep.changzhi.analyse.entity.basics.SysDepartmentInfo;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.DistrictService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import com.emep.changzhi.analyse.utils.MapToObject;
import com.emep.changzhi.analyse.utils.PageChunk;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: LFY
 * @version:V1.0
 * @Description:
 * @Date: 2018/9/5 17:22
 */

@Api(value = "区划模块")
@RestController
@RequestMapping(value = "/district")
public class DistrictIdConlltroer {
    @Autowired
    DistrictService zoningService;

    MapToObject<District> mapToObject=new MapToObject<>();

    @ApiOperation(value = "查询区划列表", notes = "传入若干参数")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean list(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex", "pageSize", "condition");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            int pageIndex = Integer.parseInt(payload.get("pageIndex").toString());
            int pageSize = Integer.parseInt(payload.get("pageSize").toString());
            Map<String, Object> condition = (Map<String, Object>) payload.get("condition");
            Page<District> districtList = zoningService.find(pageIndex, pageSize, condition);
            PageChunk<District> pageChunk = new PageChunk<>();
            if(districtList != null){
                return result.boolAdd(pageChunk.pageChunk(districtList));
            }else {
                return result.error("未查到区划列表");
            }
        }
        return result;
    }

    @ApiOperation(value = "查询区划详情", notes = "传入若干参数")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean info(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "districtId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Long districtId = Long.parseLong(payload.get("districtId").toString());
            District district = zoningService.getDistrict(districtId);
            if(district != null){
                return result.boolAdd(district);
            }else {
                return result.error("无此部门");
            }
        }
        return result;
    }

    @ApiOperation(value = "更新区划信息", notes = "传入若干参数")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean update(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "district");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("district");
                Long districtId = Long.parseLong(map.get("districtId").toString());
                if(districtId!=null) {
                    District district = zoningService.getDistrict(districtId);
                    if (district != null) {
                        boolean b = mapToObject.convertEntity(district, map);
                        if (b) {
                            boolean a = zoningService.updateDepartment(district);
                            if (a) {
                                return result.boolAdd("修改成功");
                            } else {
                                return result.error("该名称已存在");
                            }
                        } else {
                            return result.error("参数错误");
                        }
                    } else {
                        return result.boolAdd("无此部门");
                    }
                }else {
                    return result.error("zoningId为必填参数");
                }
            }
        return result;
    }

    @ApiOperation(value = "添加区划", notes = "传入若干参数")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean add(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "district");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("district");
            District district=new District();
            boolean b=mapToObject.convertEntity(district, map);
            if(b){
                boolean a = zoningService.add(district);
                if(a){
                    return result.boolAdd("新增成功");
                }else {
                    return result.error("该名称已存在");
                }
            }else {
                return result.error("参数错误");
            }
        }
        return result;
    }
    @ApiOperation(value = "删除区划", notes = "传入若干参数")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean delete(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "districtId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Long districtId = Long.parseLong(payload.get("districtId").toString());
            if(districtId!=null) {
                District district = zoningService.getDistrict(districtId);
                if (district != null) {
                    if(!district.getIsDelete().equals(Constant.RESULT_DELETECODE_YES)){
                        district.setIsDelete(Constant.RESULT_DELETECODE_YES);
                        boolean a = zoningService.deleteDistrict(district);
                        if (a) {
                            return result.boolAdd("删除成功");
                        } else {
                            return result.boolAdd("删除失败");
                        }
                    }else {
                        return result.error("此区划状态为已删除,删除失败");
                    }
                } else {
                    return result.boolAdd("无此区划");
                }
            }else {
                return result.error("districtId为必填参数");
            }
        }
        return result;
    }

}
